package cn.iocoder.yudao.module.biz.controller.admin.emergencyReservePlainDept;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.biz.controller.admin.emergencyReservePlainDept.vo.*;
import cn.iocoder.yudao.module.biz.convert.emergencyReservePlainDept.BizEmergencyReservePlainDeptConvert;
import cn.iocoder.yudao.module.biz.dal.dataobject.emergencyReservePlainDept.BizEmergencyReservePlainDeptDO;
import cn.iocoder.yudao.module.biz.service.emergencyReservePlainDept.BizEmergencyReservePlainDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 应急部门")
@RestController
@RequestMapping("/biz/emergency-reserve-plain-dept")
@Validated
public class BizEmergencyReservePlainDeptController {

    @Resource
    private BizEmergencyReservePlainDeptService emergencyReservePlainDeptService;

    @PostMapping("/create")
    @ApiOperation("创建应急部门")
    @PreAuthorize("@ss.hasPermission('biz:emergency-reserve-plain-dept:create')")
    public CommonResult<String> createEmergencyReservePlainDept(@Valid @RequestBody BizEmergencyReservePlainDeptCreateReqVO createReqVO) {
        return success(emergencyReservePlainDeptService.createEmergencyReservePlainDept(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新应急部门")
    @PreAuthorize("@ss.hasPermission('biz:emergency-reserve-plain-dept:update')")
    public CommonResult<Boolean> updateEmergencyReservePlainDept(@Valid @RequestBody BizEmergencyReservePlainDeptUpdateReqVO updateReqVO) {
        emergencyReservePlainDeptService.updateEmergencyReservePlainDept(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除应急部门")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:emergency-reserve-plain-dept:delete')")
    public CommonResult<Boolean> deleteEmergencyReservePlainDept(@RequestParam("id") String id) {
        emergencyReservePlainDeptService.deleteEmergencyReservePlainDept(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得应急部门")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:emergency-reserve-plain-dept:query')")
    public CommonResult<BizEmergencyReservePlainDeptRespVO> getEmergencyReservePlainDept(@RequestParam("id") String id) {
        BizEmergencyReservePlainDeptDO emergencyReservePlainDept = emergencyReservePlainDeptService.getEmergencyReservePlainDept(id);
        return success(BizEmergencyReservePlainDeptConvert.INSTANCE.convert(emergencyReservePlainDept));
    }

    @GetMapping("/list")
    @ApiOperation("获得应急部门列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('biz:emergency-reserve-plain-dept:query')")
    public CommonResult<List<BizEmergencyReservePlainDeptRespVO>> getEmergencyReservePlainDeptList(@RequestParam("ids") Collection<String> ids) {
        List<BizEmergencyReservePlainDeptDO> list = emergencyReservePlainDeptService.getEmergencyReservePlainDeptList(ids);
        return success(BizEmergencyReservePlainDeptConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得应急部门分页")
    @PreAuthorize("@ss.hasPermission('biz:emergency-reserve-plain-dept:query')")
    public CommonResult<PageResult<BizEmergencyReservePlainDeptRespVO>> getEmergencyReservePlainDeptPage(@Valid BizEmergencyReservePlainDeptPageReqVO pageVO) {
        PageResult<BizEmergencyReservePlainDeptDO> pageResult = emergencyReservePlainDeptService.getEmergencyReservePlainDeptPage(pageVO);
        return success(BizEmergencyReservePlainDeptConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出应急部门 Excel")
    @PreAuthorize("@ss.hasPermission('biz:emergency-reserve-plain-dept:export')")
    @OperateLog(type = EXPORT)
    public void exportEmergencyReservePlainDeptExcel(@Valid BizEmergencyReservePlainDeptExportReqVO exportReqVO,
                                                     HttpServletResponse response) throws IOException {
        List<BizEmergencyReservePlainDeptDO> list = emergencyReservePlainDeptService.getEmergencyReservePlainDeptList(exportReqVO);
        // 导出 Excel
        List<BizEmergencyReservePlainDeptExcelVO> datas = BizEmergencyReservePlainDeptConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "应急部门.xls", "数据", BizEmergencyReservePlainDeptExcelVO.class, datas);
    }

    @GetMapping("/list/all")
    @ApiOperation("应急部门列表")
//    @PreAuthorize("@ss.hasPermission('biz:emergency-reserve-plain-dept:export')")
    public CommonResult<List<BizEmergencyReservePlainDeptExcelVO>> getEmergencyReservePlainDeptListAll() {
        List<BizEmergencyReservePlainDeptDO> list = emergencyReservePlainDeptService.getAll();
        return success(BizEmergencyReservePlainDeptConvert.INSTANCE.convertList02(list));

    }

}
